package com.li.network.lab_1;

import java.io.*;
import java.net.URL;

public class ResourceReader {
    public static String read(String url) throws IOException {
        URL resource = ResourceReader.class.getClassLoader().getResource(url.substring(1));
        if (resource == null) {
            return null;
        }
        String path = resource.getPath();
        File file = new File(path);
        if (!file.exists()) {
            return null;
        }
        FileInputStream fileInputStream = new FileInputStream(file);
        InputStreamReader inputStreamReader = new InputStreamReader(fileInputStream);
        BufferedReader bufferedReader = new BufferedReader(inputStreamReader);

        StringBuffer stringBuffer = new StringBuffer();
        String content;
        while ((content=bufferedReader.readLine()) != null) {
            stringBuffer.append(content);
        }
        bufferedReader.close();
        inputStreamReader.close();
        fileInputStream.close();
        return stringBuffer.toString();
    }
}
